package com.ipay.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.util.TextUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ipay.dao.TradeRecordDao;
import com.ipay.entity.TradeRecord;
import com.ipay.redis.JedisExecutor;
import com.ipay.service.TradeRecordService;
import com.ipay.utils.TimeUtils;


@Service("tradeRecordService")
public class TradeRecordServiceImpl implements TradeRecordService {
	private Logger logger=LoggerFactory.getLogger(getClass());
	@Autowired
	private JedisExecutor jedies;
	@Autowired
	private TradeRecordDao trDao;

	@Override
	public List<TradeRecord> findTradeRecordList(Map<String, Object> map) {
		return trDao.findTradeRecordList(map);
	}

    @Override
    public List<TradeRecord> findTkTradeRecordList(Map<String, Object> map) {
        return trDao.findTkTradeRecordList(map);
    }

    @Override
	public TradeRecord findTradeRecordDetails(String recordNo) {
		// TODO Auto-generated method stub
		return trDao.findTradeRecordDetails(recordNo);
	}
    @Override
	public TradeRecord queryByRecordNo(String recordNo) {
		// TODO Auto-generated method stub
		return trDao.queryByRecordNo(recordNo);
	}

	@Override
	public List<Map<String, Object>> findDateGrouping(TradeRecord payorder) {
		// TODO Auto-generated method stub
		return trDao.findDateGrouping(payorder);
	}
	@Override
	public List<Map<String, Object>> findOrderDateStatistics(TradeRecord payorder) {
		// TODO Auto-generated method stub
		return trDao.findOrderDateStatistics(payorder);
	}
	@Override
	public List<Map<String, Object>> findDateGroupingSX(TradeRecord payorder) {
		// TODO Auto-generated method stub
		return trDao.findDateGroupingSX(payorder);
	}
	@Override
	public List<Map<String, Object>> findDateGroupingSKY(TradeRecord payorder) {
		// TODO Auto-generated method stub
		return trDao.findDateGroupingSKY(payorder);
	}

	@Override
	public List<TradeRecord> findTradeRecordPage(TradeRecord payorder) {
		return trDao.findTradeRecordPage(payorder);
	}
	@Override
	public BigDecimal findTodaySumAmount(Map<String, Object> map){
		return trDao.findTodaySumAmount(map);
	}
	@Override
	public BigDecimal findTodaySumAmountByBusId(Map<String, Object> map){
		return trDao.findTodaySumAmountByBusId(map);
	}

	@Override
	public BigDecimal findRefundSumAmount(Map<String, Object> map) {
		return trDao.findRefundSumAmount(map);
	}

	@Override
	public Integer findCountRecord(Map<String, Object> map){
		return trDao.findCountRecord(map);
	}

    @Override
	public void save(TradeRecord payorder) {
		// TODO Auto-generated method stub
		trDao.save(payorder);
	}
    
    @Override
	public List<TradeRecord> findSumAmountByDate(TradeRecord payorder){
		return trDao.findSumAmountByDate(payorder);
	}


	/***
	 *  收款员交易数据导出Excel
	 * zhenliu 2017-10-23
	 */
	public void exportExcel(String[] produceTitle,String file, List produceList) {
		FileOutputStream fileOut =null;
		try {
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet("new   sheet");
			HSSFCellStyle style = wb.createCellStyle(); // 样式对象
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平

			HSSFRow row = sheet.createRow((short) 0);
			HSSFRow row2 = sheet.createRow((short) 1);
			row.setHeight((short)366);
			for(int i=0;i<produceTitle.length;i++){
				HSSFCell ce = row.createCell((short) i);
				ce.setCellValue(produceTitle[i]); // 表格的第一行第一列显示的数据
				style.setFillBackgroundColor(HSSFColor.OLIVE_GREEN.index);//OLIVE_GREEN
				ce.setCellStyle(style); // 样式，居中
			}
			for(int i=0;i<produceList.size();i++){
				Map mapOthe = (Map) produceList.get(i);
				HSSFCell cell0 = null;
				sheet.setColumnWidth((short)i,(short)4666);
				row2 = sheet.createRow((short) (i+1));
				row2.setHeight((short)366);
				cell0 = row2.createCell((short) 0);
				cell0.setCellValue((String) mapOthe.get("name"));
				cell0.setCellStyle(style);

				List<BigDecimal> listOthe = (List<BigDecimal>) mapOthe.get("data");
				int j=1;
				for(BigDecimal bigDecimal:listOthe){
					cell0 = row2.createCell((short) j);
					cell0.setCellValue(String.valueOf(bigDecimal));
					cell0.setCellStyle(style);
					j++;
				}
			}
			fileOut = new FileOutputStream(file);
			wb.write(fileOut);
			fileOut.close();
			System.out.print("OK");
		} catch (IOException ex) {
			logger.error("收款员交易数据导出错误",ex);
			ex.printStackTrace();
		}finally {
			if(fileOut!=null) {
				try {
					fileOut.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}



}
